package com.ginage.gaugemanager.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ginage.gaugemanager.mapper.IRegisterDao;
import com.ginage.gaugemanager.mapper.entity.User;
import com.ginage.gaugemanager.service.api.IRegisterService;
import com.ginage.gaugemanager.utils.MD5;


@Service
public class RegisterServiceImpl implements IRegisterService {
	@Autowired
	IRegisterDao dao;
	
	
	@Override
	public JSONObject registerAccount(User user) {
		// TODO Auto-generated method stub
		String password=MD5.parse(user.getPassword());
		user.setPassword(password);
		user.setRegisterTime((new Date().getTime()/1000)+"");
		JSONObject jb=new JSONObject();
		int result=dao.addUser(user);
		if(result==1) {
			jb.put("status","success");
		}else {
			jb.put("status","failed");
		}
		return jb;
	}

	@Override
	public JSONObject checkUsername(String username) {
		// TODO Auto-generated method stub
		JSONObject jb=new JSONObject();
		int userCount=dao.checkUsername(username);
		if(userCount>0) {
			jb.put("status","failed");
			jb.put("info","用户名已存在!");
		}else if(userCount==0) {
			jb.put("status","success");
			
		}
		
		return jb;
	}

	@Override
	public JSONObject checkEmployeeNum(String employeeNum) {
		// TODO Auto-generated method stub
		JSONObject jb=new JSONObject();
		int employeeNumCount=dao.checkEmployeeNum(employeeNum);
		if(employeeNumCount>0) {
			jb.put("status","failed");
			jb.put("info","此工号已注册!");
		}else if(employeeNumCount==0) {
			jb.put("status","success");
			
		}
		
		return jb;
	}

}
